package br.com.wepa.webapps.search;


public class PagingBean {

	private boolean enabled = true;
	private Integer pageNumber = 1;
	private Integer pageSize = 0;
	private Integer countRows = 0;
	private String pageRequestQuery = null;
	

	// Helper Methods	

	public boolean getHasNext(){
		return getPageNumber() < getLastPageNumber();
	}
	public boolean getHasBack(){
		return getPageNumber() > 1;
	}
	
	public Integer getCountRows() {
		return countRows;
	}

	/**
	 * The number of Rows
	 * @param countRows
	 */
	public void setCountRows(Integer countRows) {
		this.countRows = countRows;
	}
	
	/**
	 * Starts in number 1 page
	 */
	public Integer getPageNumber() {
		return  pageNumber;
	}
	
	/**
	 * Starts in number 0
	 */
	public Integer getFirstRowInPage() {
		return  (getPageNumber()-1) * getPageSize() ;
	}
	
	/**
	 * Returns the next if has next pages
	 * @return
	 */
	public Integer getNextPage() {
		return   getHasNext() ? getPageNumber() + 1 : getPageNumber();
	}
	
	/**
	 * @resturn backpage number, utils the back to start page number 1
	 */
	public Integer getBackPage() {
		return  getHasBack()? getPageNumber() - 1 : getPageNumber();
	}

	public Integer getFirstPageNumber() {
		return 1;
	}
	
	public Integer getLastPageNumber() {
		return (countRows % pageSize) == 0 ? (countRows/pageSize): (countRows/pageSize + 1);    
	}

	public void setPageNumber(Integer index) {		
		this.pageNumber = index;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		if(pageSize <= 0)
			throw new IllegalArgumentException ("Mane!! nao divida por zero, uma pagina tem q ter no minimo tamanho 1");
		this.pageSize = pageSize;
	}

	public void disable() {
		enabled = false;
	}

	public void enable() {
		enabled = true;
	}

	public boolean isEnabled() {
		return enabled;
	}
	
	public boolean getEnabled() {
		return enabled;
	}


	public String getPageRequestQuery() {
		return pageRequestQuery;
	}

	public void setPageRequestQuery(String requestQuery) {
		this.pageRequestQuery = requestQuery;
	}

	/**
	 * Creates a paging bean disabled
	 * 
	 * @return
	 */
	public static PagingBean createNoPagingBean() {
		PagingBean p = new PagingBean();
		p.disable();
		return p;
	}

}
